package com.ruoyi.mall.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.enums.DeviceType;
import com.ruoyi.common.enums.UserType;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.mall.mapper.CustomerPlusMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.domain.bo.CustomerBo;
import com.ruoyi.mall.domain.vo.CustomerVo;
import com.ruoyi.mall.domain.Customer;
import com.ruoyi.mall.mapper.CustomerMapper;
import com.ruoyi.mall.service.ICustomerService;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 会员信息Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-24
 */
@RequiredArgsConstructor
@Service
public class CustomerServiceImpl implements ICustomerService {

    private final CustomerPlusMapper baseMapper;

    private final CustomerMapper customerMapper;

    /**
     * 查询会员信息
     */
    @Override
    public CustomerVo queryById(Long customerId){
        return baseMapper.selectVoById(customerId);
    }

    /**
     * 查询会员信息列表
     */
    @Override
    public TableDataInfo<CustomerVo> queryPageList(CustomerBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Customer> lqw = buildQueryWrapper(bo);
        Page<CustomerVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询会员信息列表
     */
    @Override
    public List<CustomerVo> queryList(CustomerBo bo) {
        LambdaQueryWrapper<Customer> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Customer> buildQueryWrapper(CustomerBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Customer> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCustomerLevelId() != null, Customer::getCustomerLevelId, bo.getCustomerLevelId());
        lqw.eq(bo.getLevelValue() != null, Customer::getLevelValue, bo.getLevelValue());
        lqw.like(StringUtils.isNotBlank(bo.getNickName()), Customer::getNickName, bo.getNickName());
        lqw.like(StringUtils.isNotBlank(bo.getCustomerName()), Customer::getCustomerName, bo.getCustomerName());
        lqw.eq(StringUtils.isNotBlank(bo.getPassword()), Customer::getPassword, bo.getPassword());
        lqw.eq(StringUtils.isNotBlank(bo.getCustomerIcon()), Customer::getCustomerIcon, bo.getCustomerIcon());
        lqw.eq(bo.getSex() != null, Customer::getSex, bo.getSex());
        lqw.eq(StringUtils.isNotBlank(bo.getUserCardId()), Customer::getUserCardId, bo.getUserCardId());
        lqw.eq(bo.getAge() != null, Customer::getAge, bo.getAge());
        lqw.eq(StringUtils.isNotBlank(bo.getEducation()), Customer::getEducation, bo.getEducation());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone()), Customer::getPhone, bo.getPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getIsBlack()), Customer::getIsBlack, bo.getIsBlack());
        lqw.eq(StringUtils.isNotBlank(bo.getOpenId()), Customer::getOpenId, bo.getOpenId());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), Customer::getAddress, bo.getAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getAddressCode()), Customer::getAddressCode, bo.getAddressCode());
        lqw.eq(StringUtils.isNotBlank(bo.getIpAddress()), Customer::getIpAddress, bo.getIpAddress());
        lqw.eq(bo.getVipId() != null, Customer::getVipId, bo.getVipId());
        lqw.eq(bo.getLoginTime() != null, Customer::getLoginTime, bo.getLoginTime());
        return lqw;
    }

    /**
     * 新增会员信息
     */
    @Override
    public Boolean insertByBo(CustomerBo bo) {
        Customer add = BeanUtil.toBean(bo, Customer.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setCustomerId(add.getCustomerId());
        }
        return flag;
    }

    @Override
    public void fillCustomerInfo(Customer customer)
    {
        LoginUser loginUser = new LoginUser();
        loginUser.setPhone(customer.getPhone());
        loginUser.setUserAccount(customer.getCustomerAccount());
        loginUser.setUsername(customer.getCustomerName());
        loginUser.setUserId(customer.getCustomerId());
        loginUser.setOpenId(customer.getOpenId());
        loginUser.setUserType(UserType.APP_USER.getUserType());
        LoginHelper.loginByDevice(loginUser, DeviceType.XCX);
    }

    /**
     * 修改会员信息
     */
    @Override
    public Boolean updateByBo(CustomerBo bo) {
        Customer update = BeanUtil.toBean(bo, Customer.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Customer entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除会员信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<Customer> selectCustomerList(Customer customer)
    {
        return customerMapper.selectCustomerList(customer);
    }

    @Override
    public Customer selectCustomerByCustomerId(Long customerId)
    {
        return customerMapper.selectCustomerByCustomerId(customerId);
    }


}
